package net.oschina.gitapp.widget;
import net.oschina.gitapp.R;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;
/**
* @author sunday
* 2013-12-04
*/
public class EditTextWithDel extends EditText {
private Drawable img;
private Context mContext;
public EditTextWithDel(Context context) {
super(context);
mContext = context;
init();
}
public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init();
}
public EditTextWithDel(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
private void init() {
img = mContext.getResources().getDrawable(R.drawable.edittext_delete);
addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {
setDrawable();
}
});
setDrawable();
}
//设置删除图片
private void setDrawable() {
if (length() >= 1 && this.isFocused()) {
setCompoundDrawablesWithIntrinsicBounds(null, null, img, null);
} else {
setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
}
}
/**
* 焦点改变时的处理
*/
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
setDrawable();
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
// 处理删除事件
@Override
public boolean onTouchEvent(MotionEvent event) {
if (img != null && event.getAction() == MotionEvent.ACTION_UP) {
int eventX = (int) event.getRawX();
int eventY = (int) event.getRawY();
Rect rect = new Rect();
getGlobalVisibleRect(rect);
rect.left = rect.right - 50;
if(rect.contains(eventX, eventY))
setText("");
}
return super.onTouchEvent(event);
}
@Override
protected void finalize() throws Throwable {
super.finalize();
}
}